/**
 * RenweiWu
 * RegionalStatisticsController.java
 * 2017年8月27日
 * RegionalStatisticsController
 * TODO
 * 
 */
package com.etrans.web.gps.business.report.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.TouristVehicleAreaInout;
import com.etrans.web.gps.business.report.service.RegionalStatisticsService;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年8月27日
 * @ClassName:  RegionalStatisticsController  
 * @param:   
 */


@Controller
@RequestMapping("/base/report/RegionalStatistics/")
public class RegionalStatisticsController extends BaseController{
	
	@Autowired
	private CommonService commonService;
	@Autowired
	private RegionalStatisticsService regionalStatisticsService;
	
	@RequestMapping("toRegionalStatistics.html")
	public String toRegionalStatistics(){
		return "views/business/report/regionalstatistics";
	}
	
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("getAuthCar.html")
	public @ResponseBody List getAuthCar(HttpServletRequest request){
		String name = request.getParameter("name");
		int area_id= this.getSysUser().getArea_id();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("fullID", String.format("%08d",area_id));
		param.put("vehicle_no", name);
		param.put("selected", "selected");
		List<HashMap<String,Object>> list =commonService.queryVeListByRole(param);
		return list;
	}
	
	
	@RequestMapping("getQueryEnterAndPlatByRole.html")
	public @ResponseBody List<HashMap<String,Object>> getQueryEnterAndPlatByRole(HttpServletRequest request){
 		String name = request.getParameter("name");
		int area_id= this.getSysUser().getArea_id();
		List<HashMap<String,Object>> list =commonService.queryEnterAndPlatByRole(area_id,name);
		return list;
	}
	
	@RequestMapping("getbarchart.html")
	public @ResponseBody Map<String,Object> getbarchart(String enterprise ,String startTime ,String endTime,String vehicle_no){
		int area_id = this.getSysUser().getArea_id();
		Map<String, Object> map =  regionalStatisticsService.queryEnterpirseCar(area_id, enterprise, startTime, endTime,vehicle_no);
		return map;
	}

	@RequestMapping("getpieChart.html")
	public @ResponseBody Map<String,Object> getpieChart(String enterprise_name,String startTime,String endTime,String vehicleId){
		Map<String,Object> map = regionalStatisticsService.getpieChart(enterprise_name, startTime,endTime,vehicleId);
			return map;
	}
	

	@RequestMapping("getTables.html")
	public @ResponseBody PageVo getTables(){
		Map<String,Object> map = getReqParameters();
		String paramsGrid = map.get("paramsGrid").toString();
		Map<String,Object> maps = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		return regionalStatisticsService.getTables(maps);
	}	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportRegionalStatistics.html")
	public void exportRecord( HttpServletRequest request, HttpServletResponse response,String colModel,String title,String imgsrc, String StartPage, String EndPage, String pagesize) {
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("page",Integer.parseInt(StartPage)-1);
		param.put("pageSize",(Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
		paramMap.putAll(param);
		PageVo vo= regionalStatisticsService.getTables(paramMap);
		List<TouristVehicleAreaInout> list = (List<TouristVehicleAreaInout>)vo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(list.size()-1)){
			end=list.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			TouristVehicleAreaInout obj=(TouristVehicleAreaInout)list.get(i);
			int s =obj.getVehicle_color();
			String str ="其他";
        	if(s==1){
        		str="蓝色";
        	}else if(s==2){
        		str="黄色";
        	}else if(s==3){
        		str="黑色";
        	}else if(s==4){
        		str="白色";
        	}else if(s==9){
        		str="其他";
        	}
			map.put("area_name", obj.getArea_name());
			map.put("vehicle_no",obj.getVehicle_no());
			map.put("vehicle_color",str);
			map.put("enter_time",obj.getEnter_time()==null?null:DateUtil.getDateTimeString(obj.getEnter_time(),null));
			map.put("leave_time",obj.getLeave_time()==null?null:DateUtil.getDateTimeString(obj.getLeave_time(),null));
			rows.add(map);
		}

		int index=-1;
		@SuppressWarnings("rawtypes")
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,imgsrcList);
	}
	
}